# -*- coding: utf-8 -*-
# 修正 bss mongo 里 ecuCode 字段错误的数据
# author yeyi
# date 2021-08-19

import xlwt
import pymongo
import csv
import time
import traceback
import pymysql
import json

logFileName='ecuCodeCorrect'
startTime=time.time()
TimeStr = time.strftime('%Y-%m-%d_%H-%M-%S',time.localtime(startTime))
logFileName=logFileName+TimeStr

def printt(s):
    #print(s)
    f = open(logFileName+'.log', 'a+', encoding='utf-8')
    print(s,file=f)
    print(s)
    f.close()

# 统一处理异常
def dealException(e):
    f = open(logFileName+'.log', 'a+', encoding='utf-8')
    traceback.print_exc(file=f)
    print(str(e),file=f)
    f.close()

# bss mongo 数据库地址
# 生产
mongoUrl='mongodb://mongouser:tce%40guangqi2019@10.123.16.10:27017/bss?authSource=admin'
# 预发布
# mongoUrl='mongodb://mongouser:pretce%40guangqi@10.123.2.239:27017/bss?authSource=admin'
conn = pymongo.MongoClient(mongoUrl)
db = conn['bss']
table = db.vehicleEcu

rxls = xlwt.Workbook()  # 新建工作簿
rxls.add_sheet('Sheet1', cell_overwrite_ok=True)
rxls.add_sheet('Sheet')

# vin 所属车系，为了查对应的 ecuCode 与 ecuId 关系，不同车系可能不同
series='A20'

# bss 项目 mysql 数据库登录信息
sqlHost = '10.123.16.94'
sqlPort = 3306
sqlUser = 'sirun2'
sqlPasswd = '123^54Sirun'
db = pymysql.connect(host=sqlHost,
                    port=sqlPort,
                    user=sqlUser,
                    passwd=sqlPasswd,
                    db='bss-gq',
                    charset='utf8')
cursor = db.cursor(cursor=pymysql.cursors.DictCursor)  # 使用cursor()方法获取操作游标

# 结果输出文件
f1 = open(logFileName+'Result.csv', 'w', encoding='utf-8')
csv_writer_1 = csv.writer(f1)
csv_writer_1.writerow(['vin', 'id', 'ecuCode old','ecuCode new','ecuName','old update time'])

errEcuCode='0'

def getEcus(seriesOnly):
    # SQL 查询语句
    sql = "SELECT e.ecu_code,e.ecu_name,e.ecu_describe from tab_ecu e WHERE 1=1 GROUP BY e.ecu_name ORDER BY e.ecu_name"
    if seriesOnly:
        sql = '''SELECT e.ecu_code,e.ecu_name,e.ecu_describe from tab_ecu e
                JOIN tab_vehicle_model m ON m.vehicle_model_id = e.vehicle_model_id
                JOIN tab_vehicle_series s ON s.vehicle_series_id = m.vehicle_series_id
                AND s.project_code = %s
                GROUP BY e.ecu_name
                ORDER BY e.ecu_name'''
                # 执行SQL语句
        cursor.execute(sql, (series,))
    else:
        cursor.execute(sql)

    return cursor.fetchall()

# 先用指定车系查出的 ECU code，找不到再用所有车系的 ECU code
def getEcuCode(ecuOnly,ecuAll, ecuName):
    for e in ecuOnly:
        if e['ecu_name']==ecuName:
            return e

    for e in ecuOnly:
        if ecuName in e['ecu_name']:
            printt('contain: '+ecuName+'  '+e['ecu_name'])
            return e

    printt('ecuName not exist series: '+ecuName)

    for e in ecuAll:
        if e['ecu_name']==ecuName:
            return e

    for e in ecuAll:
        if e['ecu_name'].startswith(ecuName):
            printt('containAll: '+ecuName+'  '+e['ecu_name'])
            return e

    printt('ecuName not exist all: '+ecuName)
    return None

def main():
    try:
        printt('start: '+time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(startTime)))
        printt('mongoUrl: '+mongoUrl)
        printt('sqlHost: '+sqlHost)
        printt('sqlPort: '+str(sqlPort))
        printt('sqlUser: '+sqlUser)
        printt('sqlPasswd: '+sqlPasswd)
        # 查找 ecuCode 为 0 或不存在的
        result = table.find({
            "$or":[{"ecuCode": "0",}, {"ecuCode":{"$exists": 0}}],
            "vin": {
                    "$in": [
                        "LNAD2AB30M5009449",
                        "LNADPAB14M5008868",
                        "LNAD2AB3XM5008311",
                        "LNAD2AB2XM5007988",
                        "LNAD2AB28M5007794",
                        "LNAD2AB24M5007582",
                        "LNAMBAB38M5S10029",
                        "LNAMBAB36M5S10028",
                        "LNAMBAB34M5S10027",
                        "LNAMBAB32M5S10026",
                        "LNAMBAB30M5S10025",
                        "LNAMBAB39M5S10024",
                        "LNAMBAB37M5S10023",
                        "LNAMBAB35M5S10022",
                        "LNAMJAB37M5S10039",
                        "LNAMBAB36M5S10031",
                        "LNAMBAB33M5S10035",
                        "LNAMBAB31M5S10034",
                        "LNAMJAB33M5S10040",
                        "LNAMJAB3XM5S10049",
                        "LNAMJAB38M5S10051",
                        "LNAMJAB34M5S10046",
                        "LNAMJAB37M5S10042",
                        "LNAMBAB3XM5S10033",
                        "LNAMJAB36M5S10050",
                        "LNAMJAB32M5S10045",
                        "LNAMBAB37M5S10037",
                        "LNAMJAB36M5S10047",
                        "LNAMBAB39M5S10038",
                        "LNAMJAB30M5S10044",
                        "LNAMJAB35M5S10041",
                        "LNAMJAB39M5S10043",
                        "LNAMBAB34M5S10030",
                        "LNAMJAB38M5S10048",
                        "LNAMBAB35M5S10036",
                        "LNAMBAB38M5S10032",
                        "LNAD2AB33M5005430",
                        "LNAD2AB36M5005406",
                        "LNAD2AB39M5005075",
                        "LNAD2AB34M5005033",
                        "LNAD2AB3XM5005103",
                        "LNAD2AB32M5005113",
                        "LNAD2AB3XM5005117",
                        "LNAD2AB36M5005048",
                        "LNAD2AB36M5004773",
                        "LNAD2AB20M5004243",
                        "LNAD2AB25M5002259",
                        "LNAD2AB21M5000752",
                        "LNAD2AB33M5000437",
                        "LNAD2AB20M5000418",
                        "LNAD2AB27M5000402",
                        "LNAD2AB21M5000427",
                        "LNAD2AB2XM5000426",
                        "LNAD2AB20M5000399",
                        "LNAD2AB24M5000261",
                        "LNAD2AB28M5000408",
                        "LNAD2AB22M5000386",
                        "LNAD2AB21M5000413",
                        "LNAD2AB24M5000406",
                        "LNAD2AB26M5000424",
                        "LNAD2AB2XM5000393",
                        "LNAD2AB33M5000373",
                        "LNAD2AB28M5000389",
                        "LNAD2AB3XM5000371",
                        "LNAD2AB25M5000429",
                        "LNAD2AB36M5000366",
                        "LNAD2AB25M5000379",
                        "LNAD2AB25M5000267",
                        "LNAD2AB26M5000391",
                        "LNAD2AB37M5000375",
                        "LNAD2AB28M5000392",
                        "LNAD2AB31M5000372",
                        "LNAD2AB37M5000361",
                        "LNAD2AB21M5000377",
                        "LNAD2AB34M5000365",
                        "LNAD2AB27M5000383",
                        "LNAD2AB35M5000360",
                        "LNAD2AB32M5000364",
                        "LNAD2AB30M5000363",
                        "LNAD2AB33M5000356",
                        "LNAD2AB28M5000330",
                        "LNAD2AB28M5000263",
                        "LNAD2AB24M5000339",
                        "LNAD2AB27M5000187",
                        "LNAD2AB21M5000329",
                        "LNAD2AB29M5000322",
                        "LNAD2AB20M5000337",
                        "LNAD2AB23M5000316",
                        "LNAD2AB20M5000323",
                        "LNAD2AB27M5000321",
                        "LNAD2AB22M5000338",
                        "LNAD2AB22M5000324",
                        "LNAD2AB21M5000332",
                        "LNAD2AB24M5000308",
                        "LNAD2AB26M5000309",
                        "LNAD2AB22M5000288",
                        "LNAD2AB25M5000253",
                        "LNAD2AB25M5000334",
                        "LNAD2AB27M5000335",
                        "LNAD2AB2XM5000328",
                        "LNAD2AB29M5000319",
                        "LNAD2AB22M5000310",
                        "LNAD2AB21M5000301",
                        "LNAD2AB23M5000297",
                        "LNAD2AB23M5000333",
                        "LNAD2AB25M5000317",
                        "LNAD2AB25M5000303",
                        "LNAD2AB21M5000279",
                        "LNAD2AB22M5000307",
                        "LNAD2AB29M5000305",
                        "LNAD2AB27M5000304",
                        "LNAD2AB20M5000306",
                        "LNAD2AB29M5000336",
                        "LNAD2AB26M5000326",
                        "LNAD2AB20M5000287",
                        "LNAD2AB20M5000290",
                        "LNAD2AB25M5000298",
                        "LNAD2AB21M5000296",
                        "LNAD2AB2XM5000295",
                        "LNAD2AB2XM5000314",
                        "LNAD2AB27M5000299",
                        "LNAD2AB28M5000313",
                        "LNAD2AB24M5000289",
                        "LNAD2AB29M5000286",
                        "LNAD2AB23M5000266",
                        "LNAD2AB22M5000257",
                        "LNAD2AB22M5000291",
                        "LNAD2AB2XM5000300",
                        "LNAD2AB21M5000315",
                        "LNAD2AB29M5000269",
                        "LNAD2AB2XM5000281",
                        "LNAD2AB24M5000292",
                        "LNAD2AB2XM5000278",
                        "LNAD2AB24M5000311",
                        "LNAD2AB20M5000256",
                        "LNAD2AB21M5000251",
                        "LNAD2AB20M5000273",
                        "LNAD2AB28M5000294",
                        "LNAD2AB21M5000282",
                        "LNAD2AB28M5000280",
                        "LNAD2AB24M5000275",
                        "LNAD2AB24M5000258",
                        "LNAD2AB27M5000254",
                        "LNAD2AB22M5000274",
                        "LNAD2AB29M5000272",
                        "LNAD2AB21M5000265",
                        "LNAD2AB29M5000255",
                        "LNAD2AB26M5000262",
                        "LNAD2AB22M5000260",
                        "LNAD2AB2XM5000250",
                        "LNAD2AB21M5000248",
                        "LNAD2AB26M5000245",
                        "LNAD2AB26M5000181",
                        "LNAD2AB26M5000228",
                        "LNAD2AB27M5000223",
                        "LNAD2AB2XM5000233",
                        "LNAD2AB27M5000237",
                        "LNAD2AB29M5000174",
                        "LNAD2AB22M5000209",
                        "LNAD2AB29M5000238",
                        "LNAD2AB25M5000236",
                        "LNAD2AB2XM5000247",
                        "LNAD2AB25M5000219",
                        "LNAD2AB23M5000218",
                        "LNAD2AB21M5000217",
                        "LNAD2AB22M5000226",
                        "LNAD2AB23M5000249",
                        "LNAD2AB22M5000159",
                        "LNAD2AB28M5000246",
                        "LNAD2AB21M5000170",
                        "LNAD2AB27M5000240",
                        "LNAD2AB28M5000201",
                        "LNAD2AB26M5000231",
                        "LNAD2AB20M5000242",
                        "LNAD2AB29M5000224",
                        "LNAD2AB26M5000200",
                        "LNAD2AB28M5000196",
                        "LNAD2AB22M5000212",
                        "LNAD2AB20M5000211",
                        "LNAD2AB27M5000206",
                        "LNAD2AB20M5000239",
                        "LNAD2AB21M5000234",
                        "LNAD2AB24M5000244",
                        "LNAD2AB24M5000227",
                        "LNAD2AB2XM5000202",
                        "LNAD2AB29M5000241",
                        "LNAD2AB22M5000243",
                        "LNAD2AB29M5000207",
                        "LNAD2AB21M5000220",
                        "LNAD2AB32M5000221",
                        "LNAD2AB23M5000235",
                        "LNAD2AB28M5000232",
                        "LNAD2AB28M5000215",
                        "LNAD2AB28M5000229",
                        "LNAD2AB24M5000194",
                        "LNAD2AB25M5000205",
                        "LNAD2AB25M5000186",
                        "LNAD2AB24M5000213",
                        "LNAD2AB23M5000204",
                        "LNAD2AB29M5000160",
                        "LNAD2AB25M5000169",
                        "LNAD2AB27M5000173",
                        "LNAD2AB26M5000195",
                        "LNAD2AB21M5000198",
                        "LNAD2AB2XM5000183",
                        "LNAD2AB22M5000193",
                        "LNAD2AB2XM5000166",
                        "LNAD2AB28M5000179",
                        "LNAD2AB23M5000171",
                        "LNAD2AB22M5000176",
                        "LNAD2AB24M5000180",
                        "LNAD2AB28M5000182",
                        "LNAD2AB24M5000177",
                        "LNAD2AB36M5000142",
                        "LNAD2AB21M5000167",
                        "LNAD2AB34M5000222",
                        "LNAD2AB29M5000157",
                        "LNAD2AB29M5000191",
                        "LNAD2AB29M5000188",
                        "LNAD2AB26M5000164",
                        "LNAD2AB22M5000162",
                        "LNAD2AB24M5000163",
                        "LNAD2AB26M5000178",
                        "LNAD2AB21M5000184",
                        "LNAD2AB23M5000168",
                        "LNAD2AB25M5000172",
                        "LNAD2AB20M5000161",
                        "LNAD2AB35M5000133",
                        "LNAD2AB3XM5000144",
                        "LNAD2AB35M5000150",
                        "LNAD2AB36M5000139",
                        "LNAD2AB32M5000137",
                        "LNAD2AB36M5000190",
                        "LNAD2AB39M5000149",
                        "LNAD2AB37M5000151",
                        "LNAD2AB30M5000153",
                        "LNAD2AB39M5000135",
                        "LNAD2AB38M5000143",
                        "LNAD2AB34M5000141",
                        "LNAD2AB30M5000136",
                        "LNAD2AB31M5000131",
                        "LNAD2AB33M5000146",
                        "LNAD2AB34M5000138",
                        "LNAD2AB36M5000156",
                        "LNAD2AB35M5000147",
                        "LNAD2AB24M5000101",
                        "LNAD2AB37M5000148",
                        "LNAD2AB32M5000154",
                        "LNAD2AB3XM5000189",
                        "LNAD2AB33M5000132",
                        "LNAD2AB37M5000134",
                        "LNAD2AB32M5000140",
                        "LNAD2AB39M5000152",
                        "LNAD2AB2XM5000118",
                        "LNAD2AB20M5000046",
                        "LNAD2AB33M5000129",
                        "LNAD2AB3XM5000130",
                        "LNAD2AB31M5000128",
                        "LNAD2AB3XM5000094",
                        "LNAD2AB38M5000076",
                        "LNAD2AB20M5000113",
                        "LNAD2AB29M5000109",
                        "LNAD2AB2XM5000104",
                        "LNAD2AB33M5000082",
                        "LNAD2AB21M5000119",
                        "LNAD2AB23M5000106",
                        "LNAD2AB39M5000085",
                        "LNAD2AB35M5000097",
                        "LNAD2AB22M5000100",
                        "LNAD2AB38M5000093",
                        "LNAD2AB26M5000102",
                        "LNAD2AB34M5000091",
                        "LNAD2AB29M5000112",
                        "LNAD2AB20M5000127",
                        "LNAD2AB28M5000103",
                        "LNAD2AB21M5000122",
                        "LNAD2AB28M5000120",
                        "LNAD2AB25M5000107",
                        "LNAD2AB22M5000114",
                        "LNAD2AB25M5000110",
                        "LNAD2AB33M5000079",
                        "LNAD2AB31M5000095",
                        "LNAD2AB27M5000108",
                        "LNAD2AB21M5000055",
                        "LNAD2AB29M5000126",
                        "LNAD2AB27M5000125",
                        "LNAD2AB36M5000075",
                        "LNAD2AB37M5000084",
                        "LNAD2AB35M5000083",
                        "LNAD2AB23M5000123",
                        "LNAD2AB32M5000087",
                        "LNAD2AB27M5000111",
                        "LNAD2AB22M5000050",
                        "LNAD2AB32M5000090",
                        "LNAD2AB25M5000124",
                        "LNAD2AB28M5000117",
                        "LNAD2AB24M5000115",
                        "LNAD2AB2XM5000121",
                        "LNAD2AB36M5000089",
                        "LNAD2AB34M5000088",
                        "LNAD2AB30M5000086",
                        "LNAD2AB33M5000096",
                        "LNAD2AB39M5000071",
                        "LNAD2AB37M5000070",
                        "LNAD2AB36M5000092",
                        "LNAD2AB28M5000036",
                        "LNAD2AB31M5000081",
                        "LNAD2AB2XM5000099",
                        "LNAD2AB31M5000078",
                        "LNAD2AB34M5000074",
                        "LNAD2AB28M5000098",
                        "LNAD2AB3XM5000080",
                        "LNAD2AB30M5000072",
                        "LNAD2AB26M5000052",
                        "LNAD2AB24M5000051",
                        "LNAD2AB35M5000066",
                        "LNAD2AB38M5000014",
                        "LNAD2AB37M5000022",
                        "LNAD2AB25M5000043",
                        "LNAD2AB29M5000059",
                        "LNAD2AB25M5000060",
                        "LNAD2AB27M5000058",
                        "LNAD2AB21M5000041",
                        "LNAD2AB30M5000069",
                        "LNAD2AB29M5000045",
                        "LNAD2AB2XM5000040",
                        "LNAD2AB39M5000068",
                        "LNAD2AB31M5000016",
                        "LNAD2AB23M5000042",
                        "LNAD2AB23M5000056",
                        "LNAD2AB35M5000021",
                        "LNAD2AB33M5000065",
                        "LNAD2AB37M5000067",
                        "LNAD2AB28M5000053",
                        "LNAD2AB3XM5000063",
                        "LNAD2AB25M5000057",
                        "LNAD2AB31M5000064",
                        "LNAD2AB38M5000062",
                        "LNAD2AB21M5000038",
                        "LNAD2AB27M5000061",
                        "LNAD2AB24M5000048",
                        "LNAD2AB37M5000019",
                        "LNAD2AB35M5000018",
                        "LNAD2AB33M5000017",
                        "LNAD2AB3XM5000015",
                        "LNAD2AB36M5000013",
                        "LNAD2AB33M5000020",
                        "LNAD2AB38M5000031",
                        "LNAD2AB34M5000012",
                        "LNAD2AB32M5000011",
                        "LNAD2AB36M5000030",
                        "LNAD2AB39M5000023",
                        "LNAD2AB38M5000028",
                        "LNAD2AB36M5000027",
                        "LNAD2AB2XM5000037",
                        "LNAD2AB34M5000026",
                        "LNAD2AB32M5000025",
                        "LNAD2AB24M5000034",
                        "LNAD2AB30M5000024",
                        "LNAD2AB26M5000035",
                        "LNAD2AB3XM5000029",
                        "LNAD2AB23M5000039",
                        "LNAD2AB35M5000004",
                        "LNAD2AB3XM5000001",
                        "LNAD2AB37M5000005",
                        "LNAD2AB31M5000002",
                        "LNAD2AB21M5000010",
                        "LNAD2AB23M5000008",
                        "LNAD2AB25M5000009",
                        "LNAD2AB21M5000007",
                        "LNADJAB24M5011053",
                        "LNADJAB22M5011052",
                        "LNADJAB35M5010907",
                        "LNADJAB28M5011069",
                        "LNADJAB28M5011072",
                        "LNADJAB27M5011046",
                        "LNADJAB26M5010972",
                        "LNADJAB22M5010869",
                        "LNADJAB34M5010929",
                        "LNADJAB32M5010928",
                        "LNADJAB32M5010931",
                        "LNADJAB34M5010932",
                        "LNADJAB37M5010925",
                        "LNADJAB34M5010946",
                        "LNADJAB29M5010965",
                        "LNADJAB24M5010971",
                        "LNADJAB27M5010964",
                        "LNADJAB3XM5010949",
                        "LNADJAB33M5010906",
                        "LNADJAB31M5010919",
                        "LNADJAB24M5010873",
                        "LNADJAB3XM5010918",
                        "LNADJAB38M5010948",
                        "LNADJAB32M5010945",
                        "LNADJAB30M5010944",
                        "LNADJAB30M5010927",
                        "LNADJAB37M5010939",
                        "LNADJAB35M5010938",
                        "LNADJAB33M5010937",
                        "LNADJAB3XM5010935",
                        "LNADJAB30M5010930",
                        "LNADJAB35M5010910",
                        "LNADJAB33M5010923",
                        "LNAD2AB33M5010921",
                        "LNADJAB11M5010885",
                        "LNADJAB28M5010827",
                        "LNADJAB22M5010855",
                        "LNADJAB18M5010883",
                        "LNADJAB21M5010846",
                        "LNAD2AB12M5010892",
                        "LNADJAB15M5010890",
                        "LNADJAB20M5010787",
                        "LNADJAB21M5010815",
                        "LNADJAB2XM5010814",
                        "LNADJAB21M5010832",
                        "LNADJAB24M5010825",
                        "LNADJAB21M5010877",
                        "LNADJAB29M5010836",
                        "LNADJAB13M5010886",
                        "LNADJAB2XM5010859",
                        "LNADJAB26M5010874",
                        "LNADJAB22M5010872",
                        "LNADJAB25M5010865",
                        "LNADJAB20M5010823",
                        "LNADJAB25M5010851",
                        "LNADJAB20M5010854",
                        "LNADJAB24M5010839",
                        "LNADJAB2XM5010828",
                        "LNADJAB21M5010863",
                        "LNADJAB28M5010830",
                        "LNADJAB25M5010834",
                        "LNADJAB28M5010875",
                        "LNADJAB29M5010870",
                        "LNADJAB29M5010867",
                        "LNADJAB28M5010858",
                        "LNADJAB23M5010847",
                        "LNADJAB26M5010857",
                        "LNADJAB27M5010849",
                        "LNADJAB23M5010864",
                        "LNADJAB24M5010856",
                        "LNADJAB23M5010850",
                        "LNADJAB29M5010822",
                        "LNADJAB21M5010782",
                        "LNADJAB2XM5010781",
                        "LNADJAB32M5010718",
                        "LNADJAB39M5010750",
                        "LNADJAB24M5010761",
                        "LNADJAB23M5010783",
                        "LNADJAB3XM5010739",
                        "LNADJAB33M5010730",
                        "LNADJAB25M5010798",
                        "LNADJAB37M5010715",
                        "LNADJAB23M5010816",
                        "LNADJAB24M5010811",
                        "LNADJAB26M5010812",
                        "LNADJAB27M5010608",
                        "LNADJAB29M5010769",
                        "LNADJAB27M5010768",
                        "LNADJAB35M5010731",
                        "LNADJAB3XM5010756",
                        "LNADJAB29M5010772",
                        "LNADJAB25M5010770",
                        "LNADJAB38M5010741",
                        "LNADJAB32M5010735",
                        "LNADJAB30M5010720",
                        "LNADJAB31M5010693",
                        "LNADJAB37M5010729",
                        "LNADJAB39M5010716",
                        "LNADJAB27M5010771",
                        "LNADJAB38M5010755",
                        "LNADJAB37M5010732",
                        "LNADJAB30M5010734",
                        "LNADJAB35M5010695",
                        "LNADJAB37M5010696",
                        "LNADJAB28M5010777",
                        "LNADJAB22M5010774",
                        "LNADJAB34M5010722",
                        "LNADJAB29M5010609",
                        "LNADJAB3XM5010708",
                        "LNADJAB33M5010694",
                        "LNADJAB28M5010620",
                        "LNADJAB38M5010724",
                        "LNADJAB30M5010748",
                        "LNADJAB3XM5010711",
                        "LNADJAB30M5010703",
                        "LNADJAB31M5010709",
                        "LNADJAB34M5010736",
                        "LNADJAB22M5010614",
                        "LNADJAB34M5010719",
                        "LNADJAB34M5010705",
                        "LNADJAB30M5010717",
                        "LNADJAB37M5010701",
                        "LNADJAB31M5010712",
                        "LNADJAB35M5010728",
                        "LNADJAB32M5010704",
                        "LNADJAB38M5010710",
                        "LNADJAB20M5010627",
                        "LNADJAB28M5010634",
                        "LNADJAB24M5010601",
                        "LNADJAB20M5010594",
                        "LNADJAB23M5010606",
                        "LNADJAB29M5010593",
                        "LNADJAB25M5010591",
                        "LNADJAB28M5010603",
                        "LNADJAB2XM5010604",
                        "LNADJAB29M5010626",
                        "LNADJAB27M5010625",
                        "LNADJAB22M5010631",
                        "LNADJAB24M5010629",
                        "LNADJAB19M5010682",
                        "LNADJAB2XM5010635",
                        "LNADJAB15M5010677",
                        "LNADJAB26M5010650",
                        "LNADJAB25M5010655",
                        "LNADJAB22M5010628",
                        "LNADJAB19M5010679",
                        "LNADJAB21M5010622",
                        "LNADJAB25M5010607",
                        "LNADJAB2XM5010618",
                        "LNADJAB22M5010595",
                        "LNAD2AB34M5010698",
                        "LNAD2AB33M5010689",
                        "LNAD2AB23M5010666",
                        "LNAD2AB38M5010686",
                        "LNAD2AB31M5010688",
                        "LNAD2AB27M5010637",
                        "LNADJAB36M5010530",
                        "LNADJAB25M5010557",
                        "LNADJAB24M5010551",
                        "LNADJAB38M5010531",
                        "LNADJAB22M5010547",
                        "LNADJAB21M5010619",
                        "LNADJAB29M5010187",
                        "LNADJAB24M5010436",
                        "LNADJAB22M5010399",
                        "LNADJAB28M5010388",
                        "LNADJAB3XM5010532",
                        "LNADJAB3XM5010529",
                        "LNADJAB32M5010525",
                        "LNADJAB31M5010516",
                        "LNADJAB26M5010552",
                        "LNADJAB36M5010527",
                        "LNADJAB20M5010563",
                        "LNADJAB2XM5010599",
                        "LNADJAB26M5010549",
                        "LNADJAB27M5010592",
                        "LNADJAB33M5010498",
                        "LNADJAB28M5010598",
                        "LNADJAB26M5010602",
                        "LNADJAB30M5010524",
                        "LNADJAB21M5010605",
                        "LNADJAB2XM5010621",
                        "LNADJAB36M5010494",
                        "LNADJAB3XM5010482",
                        "LNADJAB33M5010503",
                        "LNADJAB3XM5010501",
                        "LNADJAB38M5010500",
                        "LNAD2AB30M5010522",
                        "LNADJAB20M5010580",
                        "LNADJAB28M5010617",
                        "LNADJAB24M5010615",
                        "LNADJAB24M5010596",
                        "LNADJAB22M5010600",
                        "LNADJAB38M5010478",
                        "LNADJAB34M5010476",
                        "LNADJAB39M5010473",
                        "LNADJAB31M5010483",
                        "LNADJAB31M5010502",
                        "LNAD2AB38M5010493",
                        "LNADJAB29M5010562",
                        "LNADJAB32M5010539",
                        "LNADJAB21M5010555",
                        "LNADJAB2XM5010554",
                        "LNADJAB25M5010560",
                        "LNADJAB29M5010559",
                        "LNADJAB35M5010518",
                        "LNADJAB32M5010475",
                        "LNADJAB30M5010507",
                        "LNADJAB37M5010505",
                        "LNADJAB32M5010508",
                        "LNADJAB34M5010509",
                        "LNADJAB39M5010506",
                        "LNADJAB34M5010526",
                        "LNADJAB33M5010517",
                        "LNADJAB29M5010383",
                        "LNADJAB35M5010308",
                        "LNADJAB24M5010405",
                        "LNADJAB21M5010393",
                        "LNADJAB21M5010412",
                        "LNADJAB25M5010364",
                        "LNADJAB22M5010385",
                        "LNADJAB38M5010285",
                        "LNADJAB27M5010396",
                        "LNADJAB29M5010366",
                        "LNAD2AB20M5010589",
                        "LNADJAB31M5010497",
                        "LNADJAB38M5010495",
                        "LNADJAB38M5010481",
                        "LNADJAB30M5010474",
                        "LNADJAB33M5010484",
                        "LNADJAB2XM5010389",
                        "LNADJAB35M5010485",
                        "LNADJAB36M5010477",
                        "LNADJAB3XM5010479",
                        "LNADJAB30M5010300",
                        "LNADJAB21M5010359",
                        "LNADJAB35M5010311",
                        "LNADJAB35M5010339",
                        "LNADJAB30M5010295",
                        "LNADJAB3XM5010336",
                        "LNADJAB35M5010499",
                        "LNADJAB29M5010397",
                        "LNADJAB38M5010299",
                        "LNADJAB33M5010324",
                        "LNADJAB38M5010321",
                        "LNADJAB22M5010208",
                        "LNAD2AB29M5010428",
                        "LNADJAB29M5010416",
                        "LNADJAB25M5010414",
                        "LNADJAB20M5010367",
                        "LNADJAB27M5010379",
                        "LNADJAB25M5010378",
                        "LNADJAB28M5010357",
                        "LNADJAB2XM5010375",
                        "LNADJAB31M5010290",
                        "LNADJAB20M5010353",
                        "LNADJAB27M5010365",
                        "LNADJAB26M5010356",
                        "LNADJAB34M5010302",
                        "LNADJAB27M5010401",
                        "LNADJAB23M5010377",
                        "LNADJAB20M5010420",
                        "LNADJAB22M5010368",
                        "LNADJAB24M5010422",
                        "LNADJAB27M5010429",
                        "LNADJAB22M5010435",
                        "LNADJAB2XM5010425",
                        "LNADJAB20M5010403",
                        "LNADJAB22M5010421",
                        "LNADJAB2XM5010411",
                        "LNADJAB23M5010394",
                        "LNADJAB21M5010426",
                        "LNADJAB23M5010380",
                        "LNADJAB28M5010410",
                        "LNADJAB34M5010297",
                        "LNADJAB3XM5010319",
                        "LNADJAB36M5010334",
                        "LNADJAB32M5010301",
                        "LNADJAB32M5010315",
                        "LNADJAB37M5010312",
                        "LNADJAB30M5010314",
                        "LNADJAB36M5010320",
                        "LNADJAB38M5010318",
                        "LNADJAB3XM5010305",
                        "LNADJAB18M5010270",
                        "LNADJAB36M5010298",
                        "LNADJAB3XM5010286",
                        "LNADJAB31M5010306",
                        "LNADJAB33M5010307",
                        "LNADJAB31M5010077",
                        "LNADJAB35M5010292",
                        "LNADJAB33M5010291",
                        "LNADJAB33M5010288",
                        "LNADJAB37M5010309",
                        "LNADJAB24M5010209",
                        "LNADJAB29M5010237",
                        "LNADJAB2XM5010215",
                        "LNADJAB38M5010304",
                        "LNADJAB25M5010204",
                        "LNADJAB22M5010130",
                        "LNADJAB13M5010273",
                        "LNADJAB22M5010211",
                        "LNADJAB21M5010216",
                        "LNADJAB29M5010206",
                        "LNADJAB27M5010205",
                        "LNADJAB24M5010260",
                        "LNADJAB20M5010238",
                        "LNADJAB24M5010212",
                        "LNADJAB21M5010202",
                        "LNADJAB25M5010199",
                        "LNADJAB20M5010210",
                        "LNADJAB1XM5010271",
                        "LNADJAB20M5010207",
                        "LNADJAB27M5010186",
                        "LNADJAB19M5010276",
                        "LNADJAB20M5010191",
                        "LNADJAB28M5010178",
                        "LNADJAB25M5010252",
                        "LNADJAB22M5010175",
                        "LNADJAB23M5010184",
                        "LNADJAB2XM5010232",
                        "LNADJAB29M5010190",
                        "LNADJAB28M5010245",
                        "LNADJAB22M5010239",
                        "LNADJAB24M5010176",
                        "LNADJAB27M5010236",
                        "LNADJAB2XM5010201",
                        "LNADJAB11M5010269",
                        "LNADJAB26M5010244",
                        "LNADJAB28M5010200",
                        "LNADJAB21M5010183",
                        "LNADJAB25M5010221",
                        "LNADJAB2XM5010182",
                        "LNADJAB28M5010181",
                        "LNADJAB2XM5010179",
                        "LNADJAB23M5010220",
                        "LNADJAB11M5010272",
                        "LNADJAB23M5010217",
                        "LNADJAB26M5010213",
                        "LNADJAB22M5010189",
                        "LNADJAB25M5010218",
                        "LNADJAB26M5010177",
                        "LNADJAB10M5010277",
                        "LNADJAB20M5010174",
                        "LNADJAB26M5010115",
                        "LNADJAB39M5010098",
                        "LNADJAB3XM5010076",
                        "LNADJAB24M5010114",
                        "LNADJAB23M5010170",
                        "LNADJAB22M5010032",
                        "LNADJAB2XM5010151",
                        "LNADJAB39M5010070",
                        "LNADJAB27M5010169",
                        "LNADJAB26M5010129",
                        "LNADJAB34M5010090",
                        "LNADJAB33M5010100",
                        "LNADJAB30M5010085",
                        "LNADJAB23M5010122",
                        "LNADJAB26M5010132",
                        "LNADJAB20M5010126",
                        "LNADJAB21M5010118",
                        "LNADJAB32M5010069",
                        "LNADJAB32M5010072",
                        "LNADJAB33M5010078",
                        "LNADJAB37M5010066",
                        "LNADJAB25M5010123",
                        "LNADJAB28M5010116",
                        "LNADJAB28M5010150",
                        "LNADJAB30M5010068",
                        "LNADJAB29M5010125",
                        "LNADJAB27M5010124",
                        "LNADJAB21M5010121",
                        "LNADJAB2XM5010120",
                        "LNADJAB39M5010067",
                        "LNADJAB25M5010171",
                        "LNADJAB34M5010073",
                        "LNADJAB29M5010173",
                        "LNADJAB23M5010119",
                        "LNADJAB35M5010065",
                        "LNADJAB30M5010071",
                        "LNADJAB27M5010172",
                        "LNADJAB2XM5010117",
                        "LNADJAB26M5009952",
                        "LNADJAB28M5009984",
                        "LNADJAB21M5009972",
                        "LNADJAB20M5010028",
                        "LNADJAB20M5010031",
                        "LNADJAB29M5010030",
                        "LNADJAB20M5010000",
                        "LNADJAB31M5S70835",
                        "LNADJAB23M5009973",
                        "LNADJAB25M5009943",
                        "LNADJAB2XM5009999",
                        "LNADJAB22M5009950",
                        "LNADJAB26M5009997",
                        "LNADJAB20M5009977",
                        "LNADJAB23M5009956",
                        "LNADJAB2XM5009971",
                        "LNADJAB20M5009994",
                        "LNADJAB27M5009992",
                        "LNADJAB22M5009978",
                        "LNADJAB28M5009998",
                        "LNADJAB22M5010029",
                        "LNADJAB25M5009957",
                        "LNADJAB29M5010027",
                        "LNADJAB22M5009995",
                        "LNADJAB25M5009988",
                        "LNADJAB29M5009993",
                        "LNADJAB27M5010026",
                        "LNADJAB22M5010001",
                        "LNADJAB24M5009951",
                        "LNADJAB27M5009958",
                        "LNADJAB24M5009996",
                        "LNADJAB28M5009970",
                        "LNADJAB23M5009942",
                        "LNADJAB27M5009975",
                        "LNADJAB21M5009986",
                        "LNADJAB24M5009965",
                        "LNADJAB26M5009983",
                        "LNADJAB27M5009989",
                        "LNADJAB29M5009976",
                        "LNADJAB22M5009947",
                        "LNADJAB23M5009987",
                        "LNADJAB2XM5009968",
                        "LNADJAB25M5009974",
                        "LNADJAB22M5009964",
                        "LNADJAB23M5009990",
                        "LNADJAB24M5009982",
                        "LNADJAB27M5009961",
                        "LNADJAB36M5009894",
                        "LNADJAB25M5009991",
                        "LNADJAB21M5009955",
                        "LNADJAB2XM5009985",
                        "LNADJAB29M5009959",
                        "LNADJAB28M5009967",
                        "LNADJAB26M5009966",
                        "LNADJAB29M5009962",
                        "LNADJAB22M5009981",
                        "LNADJAB20M5009963",
                        "LNADJAB21M5009969",
                        "LNADJAB27M5009944",
                        "LNADJAB28M5009953"
                ]
            }
        }).sort([("updateDate",-1)])
        mongoResult=list(result)
        siz=len(mongoResult)
        printt('size: '+str(siz))
        if siz<=0:
            printt('size 0')
            return

        ecuSeriesOnly=getEcus(True)
        printt('ecuSeriesOnly: '+json.dumps(ecuSeriesOnly))
        ecuAll=getEcus(False)
        printt('ecuAll: '+json.dumps(ecuAll))
        for re in mongoResult:
            try:
                ecu=getEcuCode(ecuSeriesOnly, ecuAll, re['ecuName'])
                if ecu is None:
                    continue

                table.update_one({"_id":re['_id']},{"$set":{"ecuCode":ecu['ecu_code'],"updateDate":int(round(startTime*1000))}})
                ecuCodeOld='not exist'
                if 'ecuCode' in re:
                    ecuCodeOld=re['ecuCode']
                line = [re['vin'], re['_id'], ecuCodeOld,ecu['ecu_code'],re['ecuName'],re['updateDate']]
                csv_writer_1.writerow(line)
            except:
                # 打印错误信息
                printt('for err: '+str(re))
                traceback.print_exc()
    except Exception as e:
        dealException(e)
    finally:
        db.close()
        f1.close()
        printt('end: '+time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))

if __name__ == '__main__':
    main()